package org.example.fronted.controller;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import org.example.entity.Data;
import org.example.entity.WebOrders;
import org.example.fronted.service.OrderService;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
public class OrderController {

    @Resource
    private OrderService orderService;

    @RequestMapping("/ensureOrder")
    public Object ensureOrder(HttpServletRequest req, HttpSession session) {

        // 获取商品的id以及商品的数量: {'1001':1, '1002':1}
        String arr = req.getParameter("arr");
        System.out.println("arr" + arr);

        // 将json字符串转成数组
        JSONArray jsonArray = JSONArray.parseArray(arr);
        System.out.println(jsonArray.size());

        Map<Integer, Integer> goodInfos = new  HashMap<>();
        for (int i = 0; i < jsonArray.size(); i++) {
            // 遍历数组，转成Json字符串
            String s = jsonArray.get(i).toString();
            // 将Json字符串转化为Json对象
            JSONObject obj = JSON.parseObject(s);
            // 从Json对象中通过key提取value
            String goodsId = (String) obj.get("goodsId");
            String num = (String) obj.get("num");
            // 放入map集合中
            goodInfos.put(Integer.parseInt(goodsId),Integer.parseInt(num));
        }
        goodInfos.forEach((k,v) -> {
            System.out.println("goodsId" + k + ",num" + v);
        });

        Integer userId = (Integer) req.getSession().getAttribute("userId");

        // 默认的收货地址id
        String takeDelivertyAddressId = req.getParameter("takeDeliveryAddressId");
        System.out.println("takeDelivertyAddressId:" + takeDelivertyAddressId);

        // 将json类型的字符串反序列化为 Java对象
        // Map<String, Integer> goodsInfo = JSONObject.parseObject(arr, Map.class);
//        Map<Integer, Integer> goodInfos = new HashMap<>();
//        goodsInfo.forEach((k, v) -> {
//            goodInfos.put(Integer.parseInt(k), v);
//            System.out.println("goodsId" + k + ",goodsNum:" + v);
//        });

//        Integer userId = 28;
//        if (id != null){
//            userId = Integer.parseInt(id);  //获取用户id
//        }

        Integer takeDeliveryAddressId = 0;

//        if (takeDelivertyAddressId != null) {
//            takeDeliveryAddressId = Integer.parseInt(takeDelivertyAddressId);
//        }

        Data data = orderService.ensureOrder(goodInfos, Integer.parseInt(takeDelivertyAddressId), userId);


        return data;

    }

    /**
     * 用户在结算页面点击 “确认支付” 的时候调用
     * @param req
     */
    @RequestMapping("/confirmPay")
    public Object confirmPay(HttpServletRequest req) {
        String orderNo = req.getParameter("oNo"); //获取订单编号

        String uid = req.getParameter("uid");
        System.out.println(uid);

        orderNo = "20200526152643443224802";
        Integer userId = 28;  //获取用户id

//        Data data = orderService.confirmPay(orderNo, Integer.parseInt(uid));
        Data data = orderService.confirmPay(orderNo, userId);

        return data;
    }

    @RequestMapping("/getOrderOfStatus")
    public List<WebOrders> getOrderOfStatus(HttpServletRequest req) {
//        String status = req.getParameter("status");

//        HttpSession session = req.getSession();

//        User user = (User)session.getAttribute("user");

//        Integer userId = user.getId();  //获取用户id
        Integer userId = 28;  //获取用户id
        Integer status = 0;

        List<WebOrders> orderList = orderService.getOrdersByStatus(status, userId);

        return orderList;
    }

}
